package cn.doourbest.learnspring.security;

import cn.doourbest.learnspring.config.LearnSpringConfig;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;


public class SecurityWebApplicationInitializer
    extends AbstractSecurityWebApplicationInitializer {

    public SecurityWebApplicationInitializer() {
        super(CustomerWebSecurityConfig.class);
    }

    @Override
    protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext aa
                = new AnnotationConfigWebApplicationContext();
        aa.register(LearnSpringConfig.class);
        aa.setServletContext(servletContext);

        DispatcherServlet dispatcherServlet = new DispatcherServlet(aa);
        dispatcherServlet.setEnableLoggingRequestDetails(true);
        ServletRegistration.Dynamic ds = servletContext.addServlet("dispatcherServlet",dispatcherServlet);
        ds.addMapping("/*");
        ds.setLoadOnStartup(1);

        super.beforeSpringSecurityFilterChain(servletContext);
    }
}